package gov.mogaha.ntis.cmm.mob.view;

import gov.mogaha.ntis.cmm.mob.R;
import gov.mogaha.ntis.cmm.mob.util.Logger;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class SelectButtonView extends LinearLayout {

	public final static int CLICK_LEFT = 1;
	public final static int CLICK_RIGHT = 0;
	
	private final Context mContext;
	private Button mBtnLeft;
	private Button mBtnRight;
	
	private int mIsSelect;
	
	private boolean mIsClickable;
	
	public SelectButtonView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
		// TODO Auto-generated constructor stub
	}
	
	public SelectButtonView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		init();
	}

	private void init(){
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.selectbuttonview, null);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));	
		
		mIsSelect = CLICK_LEFT;
		
		mBtnLeft = (Button)layout.findViewById(R.id.btn_left);
		mBtnLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mIsClickable) setButton(SelectButtonView.CLICK_LEFT);
			}
		});
		mBtnRight = (Button)layout.findViewById(R.id.btn_right);
		mBtnRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mIsClickable) setButton(SelectButtonView.CLICK_RIGHT);
			}
		});

		addView(layout);
		
		try{
			mIsClickable = false;
			String attr = (String)getTag();
			if("clickable".equals(attr)){
				mIsClickable = true;
			}
		}catch(Exception e){
			Logger.e(e.toString());
		}
	}
	
	public void setButton(int value){
		mIsSelect = value;
		setBackground();
	}
	
	private void setBackground(){
		if(mIsSelect==SelectButtonView.CLICK_LEFT){
			mBtnLeft.setBackgroundResource(R.drawable.button03_nine);
			mBtnRight.setBackgroundResource(R.drawable.button04_nine);
		} else{
			mIsSelect=SelectButtonView.CLICK_RIGHT;
			mBtnLeft.setBackgroundResource(R.drawable.button04_nine);
			mBtnRight.setBackgroundResource(R.drawable.button03_nine);
		}
	}
	
	public int getValue(){
		return mIsSelect;
	}
	
	public void setLeftText(String str){
		mBtnLeft.setText(str);
	}
	
	public void setRightText(String str){
		mBtnRight.setText(str);
	}
	
	public void setClickable(boolean val){
		mIsClickable = val;
	}

}
